package com.burntsky.xbmc.android;


import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

import org.xbmc.android.remote.business.Command;
import org.xbmc.android.remote.business.ManagerFactory;
import org.xbmc.android.remote.business.TvShowManager;
import org.xbmc.android.util.ClientFactory;
import org.xbmc.android.util.HostFactory;
import org.xbmc.api.business.DataResponse;
import org.xbmc.api.business.INotifiableManager;
import org.xbmc.api.business.ITvShowManager;
import org.xbmc.api.object.Host;
import org.xbmc.api.object.TvShow;
import org.xbmc.api.presentation.INotifiableController;
import org.xbmc.httpapi.*;

import com.burntsky.xbmc.android.TvShowListFragment.OnTvShowSelectedListener;

public class HomeActivity extends Activity  {
	
	//Button mMyButton;
	protected XbmcController _Controller;
	private Category _activeCategory;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        this.setContentView(R.layout.home);

        this._Controller = new XbmcController(this.getApplicationContext());
        CategoryManager.Initialize(this._Controller);
        
        View rightPane = this.findViewById(R.id.right_pane);
        rightPane.setVisibility(View.GONE);
       
        FragmentManager fragMgr = this.getFragmentManager();
        FragmentTransaction txn = fragMgr.beginTransaction();
        txn.add(R.id.left_pane, new ActivitySelectionListFragment(this._Controller));
        //txn.add(R.id.center_pane, new TvShowListFragment(this._Controller));
        //txn.add(R.id.right_pane, new TvDetailsFragment(this._Controller));
        txn.commit();
        
        this._activeCategory = CategoryManager.GetCategories().get(0);
        this.ShowCategory(this._activeCategory);

        
         
        ActionBar bar = getActionBar();
        
        //bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(true);
        bar.setTitle(R.string.app_name);
    }
    

    
    public void ShowCategory(Category category){
    	Toast.makeText(getApplicationContext(), category.getName(), Toast.LENGTH_LONG).show();
    	
    	FragmentManager fragMgr = this.getFragmentManager();
    	FragmentTransaction txn = fragMgr.beginTransaction();
    	
    	if(category.getId() == 2){
    		txn.replace(R.id.center_pane, new MovieListFragment(this._Controller));
    		//txn.remove(fragment)
    	}
    	else if(category.getId() == 1){
    		txn.replace(R.id.center_pane, new TvShowListFragment(this._Controller));
    	}
    	txn.addToBackStack(null);
    	txn.commit();
    	
    	this._activeCategory = category;
    }
    
    public void ShowDetail(String detail){
    	Utils.Alert(this, detail);
    }
        
    /* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		//return super.onCreateOptionsMenu(menu);
		
		MenuInflater mi = this.getMenuInflater();
		mi.inflate(R.menu.main_menu, menu);
		
		//menu.add("menu 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
		return true;
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in Action Bar clicked; go home
                Intent intent = new Intent(this, HomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            case R.id.menu_about:
            	this.showNotification();
            	return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
	

    void showNotification() {
        final Resources res = getResources();
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_notify_example)
                .setAutoCancel(true)
                .setTicker("ticker text")
                .setContentIntent(getDialogPendingIntent("Tapped the notification entry."));

            builder
                    .setNumber(7) // An example number.
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("content text");

        notificationManager.notify(1, builder.getNotification());
    }
	

    PendingIntent getDialogPendingIntent(String dialogText) {
        return PendingIntent.getActivity(
                this,
                dialogText.hashCode(), // Otherwise previous PendingIntents with the same
                                       // requestCode may be overwritten.
                new Intent("com.xxx")
                        .putExtra(Intent.EXTRA_TEXT, dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                0);
    }
    
}

